package com.vdong.rms.service.impl;

import com.vdong.rms.entity.domain.supper.SupperDO;
import com.vdong.rms.entity.mo.*;
import com.vdong.rms.entity.mo.System;
import com.vdong.rms.entity.parameter.*;
import com.vdong.rms.repository.dao.*;
import com.vdong.rms.service.PermissionManageService;
import com.vdong.rms.service.function.OperationFunction;
import org.apache.commons.collections.CollectionUtils;
import org.lamb.framework.common.util.BeanPlasticityUtill;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.StringUtil;
import org.lamb.framework.common.util.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @description: 权限管理
 * @author: Mr.WangGang
 * @create: 2018-10-23 上午 9:47
 **/
@Service
public class PermissionManageServiceImpl implements PermissionManageService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RmsServiceMapper rmsServiceMapper;

    @Autowired
    private SystemMapper systemMapper;

    @Autowired
    private RuleMapper ruleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleServiceMapper roleServiceMapper;

    @Autowired
    private SystemServiceMapper systemServiceMapper;

    @Autowired
    private RuleRoleMapper ruleRoleMapper;

    @Override
    public void operationUser(OperationUserPO operationUserPO) {
        ValidatorUtil.validate(operationUserPO);
        User user = CopyUtil.copyObject(operationUserPO, new User());
        user.setCreateTime(new Date());
        userMapper.insertSelective(user);
    }

    @Override
    public void operationRole(OperationRolePO operationRolePO) {
        ValidatorUtil.validate(operationRolePO);
        Role role = CopyUtil.copyObject(operationRolePO, new Role());
        role.setCreateTime(new Date());
        roleMapper.insertSelective(role);
    }

    @Override
    public void operationService(OperationServicePO operationServicePO) {
        ValidatorUtil.validate(operationServicePO);
        RmsService rmsService = CopyUtil.copyObject(operationServicePO, new RmsService());
        rmsService.setCreateTime(new Date());
        rmsServiceMapper.insertSelective(rmsService);
    }

    @Override
    public void operationSystem(OperationSystemPO operationSystemPO) {
        ValidatorUtil.validate(operationSystemPO);
        System system = CopyUtil.copyObject(operationSystemPO, new System());
        system.setCreateTime(new Date());
        systemMapper.insertSelective(system);
    }

    @Override
    public void operationRule(OperationRulePO operationRulePO) {
        ValidatorUtil.validate(operationRulePO);
        Rule rule = CopyUtil.copyObject(operationRulePO, new Rule());
        rule.setCreateTime(new Date());
        ruleMapper.insertSelective(rule);
    }

    @Override
    public void relateUserToRole(RelateUserToRolePO relateUserToRolePO) {
        ValidatorUtil.validate(relateUserToRolePO);

        relateUserToRolePO.getRoleIds().stream().filter(roleId -> !StringUtil.hasBlank(roleId)).forEach((roleId) -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(Integer.valueOf(relateUserToRolePO.getUserId()));
            userRole.setRoleId(Integer.valueOf(roleId));
            userRole.setCreateTime(new Date());
            userRoleMapper.insertSelective(userRole);
        });
    }

    @Override
    public void relateRoleToService(RelateRoleToServicePO relateRoleToServicePO) {
        ValidatorUtil.validate(relateRoleToServicePO);

        relateRoleToServicePO.getServiceIds().stream().filter(serviceId -> !StringUtil.hasBlank(serviceId)).forEach(serviceId -> {
            RoleService roleService = new RoleService();
            roleService.setRoleId(Integer.valueOf(relateRoleToServicePO.getRoleId()));
            roleService.setServiceId(Integer.valueOf(serviceId));
            roleService.setCreateTime(new Date());
            roleServiceMapper.insertSelective(roleService);
        });


    }


    @Override
    public void relateSystemToService(RelateSystemToServicePO relateSystemToServicePO) {
        ValidatorUtil.validate(relateSystemToServicePO);


        relateSystemToServicePO.getServiceIds().stream().filter(serviceId -> !StringUtil.hasBlank(serviceId)).forEach((serviceId) -> {
            SystemService systemService = new SystemService();
            systemService.setSystemId(Integer.valueOf(relateSystemToServicePO.getSystemId()));
            systemService.setServiceId(Integer.valueOf(serviceId));
            systemService.setCreateTime(new Date());
            systemServiceMapper.insertSelective(systemService);

        });
    }

    @Override
    public void relateRuleToRole(RelateRuleToRolePO relateRuleToRolePO) {
        ValidatorUtil.validate(relateRuleToRolePO);

        relateRuleToRolePO.getRoleIds().stream().filter(roleId -> !StringUtil.hasBlank(roleId)).forEach((roleId) -> {
            RuleRole ruleRole = new RuleRole();
            ruleRole.setRuleId(Integer.valueOf(relateRuleToRolePO.getRuleId()));
            ruleRole.setRoleId(Integer.valueOf(roleId));
            ruleRole.setCreateTime(new Date());
            ruleRoleMapper.insertSelective(ruleRole);

        });
    }

    @Override
    public void deleteUser(DeleteUserPO deleteUserPO) {
        ValidatorUtil.validate(deleteUserPO);

        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(Integer.valueOf(deleteUserPO.getUserId()));
        List<UserRole> userRoleList = userRoleMapper.selectByExample(userRoleExample);
        //删除用户与角色关联表
        userRoleList.forEach(userRole -> userRoleMapper.deleteByPrimaryKey(userRole.getUserRoleId()));

        //删除用户
        userMapper.deleteByPrimaryKey(Integer.valueOf(deleteUserPO.getUserId()));

        /*operation(deleteUserPO, UserDO.class, userDO -> {
            //删除用户与角色关联表
            List<UserRoleDO> userRoleDOList = userRoleDORepository.findByUserId(userDO.getUserId()).orElse(Lists.newArrayList());
            userRoleDOList.stream().filter(Objects::nonNull).forEach((userRoleDO) ->
                    userRoleMapper.deleteByPrimaryKey(userRoleDO.getUserRoleId()));
            //删除用户
            userMapper.deleteByPrimaryKey(userDO.getUserId());
        });*/
    }

    @Override
    public void deleteRole(DeleteRolePO deleteRolePO) {
        ValidatorUtil.validate(deleteRolePO);

        RoleServiceExample roleServiceExample = new RoleServiceExample();
        roleServiceExample.createCriteria().andRoleIdEqualTo(Integer.valueOf(deleteRolePO.getRoleId()));
        List<RoleService> roleServiceList = roleServiceMapper.selectByExample(roleServiceExample);
        //删除角色与服务关联表
        roleServiceList.forEach(roleService -> roleServiceMapper.deleteByPrimaryKey(roleService.getRoleServiceId()));


        RuleRoleExample ruleRoleExample = new RuleRoleExample();
        ruleRoleExample.createCriteria().andRoleIdEqualTo(Integer.valueOf(deleteRolePO.getRoleId()));
        List<RuleRole> ruleRoleList = ruleRoleMapper.selectByExample(ruleRoleExample);
        //删除规则与角色关联表
        ruleRoleList.forEach(ruleRole -> ruleRoleMapper.deleteByPrimaryKey(ruleRole.getRuleRoleId()));

        //删除角色
        roleMapper.deleteByPrimaryKey(Integer.valueOf(deleteRolePO.getRoleId()));

        /*operation(deleteRolePO, RoleDO.class, roleDO -> {
            //删除角色与服务关联表
            List<RoleServiceDO> roleServiceDOList = roleServiceDORepository.findByRoleId(roleDO.getRoleId()).orElse(Lists.newArrayList());
            roleServiceDOList.stream().filter(Objects::nonNull).forEach((roleServiceDO) ->
                    roleServiceMapper.deleteByPrimaryKey(roleServiceDO.getRoleServiceId()));
            //删除规则与角色关联表
            List<RuleRoleDO> ruleRoleDOList = ruleRoleDORepository.findByRoleId(roleDO.getRoleId()).orElse(Lists.newArrayList());
            ruleRoleDOList.stream().filter(Objects::nonNull).forEach((ruleRoleDO) ->
                    ruleRoleMapper.deleteByPrimaryKey(ruleRoleDO.getRuleRoleId()));
            //删除角色
            roleMapper.deleteByPrimaryKey(roleDO.getRoleId());
        });*/
    }

    @Override
    public void deleteService(DeleteServicePO deleteServicePO) {
        ValidatorUtil.validate(deleteServicePO);


        SystemServiceExample example = new SystemServiceExample();
        example.createCriteria().andServiceIdEqualTo(Integer.valueOf(deleteServicePO.getServiceId()));
        List<SystemService> systemServiceList = systemServiceMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(systemServiceList)) {
            return;
        }
        //删除系统与服务关联表
        systemServiceMapper.deleteByPrimaryKey(systemServiceList.get(0).getSystemServiceId());


        RoleServiceExample roleServiceExample = new RoleServiceExample();
        roleServiceExample.createCriteria().andServiceIdEqualTo(Integer.valueOf(deleteServicePO.getServiceId()));
        List<RoleService> roleServiceList = roleServiceMapper.selectByExample(roleServiceExample);

        //删除角色与服务关联表
        roleServiceList.forEach(roleService -> roleServiceMapper.deleteByPrimaryKey(roleService.getRoleServiceId()));

        //删除服务
        rmsServiceMapper.deleteByPrimaryKey(Integer.valueOf(deleteServicePO.getServiceId()));
    }

    @Override
    public void deleteSystem(DeleteSystemPO deleteSystemPO) {
        ValidatorUtil.validate(deleteSystemPO);

        SystemServiceExample example = new SystemServiceExample();
        example.createCriteria().andSystemIdEqualTo(Integer.valueOf(deleteSystemPO.getSystemId()));
        List<SystemService> systemServiceList = systemServiceMapper.selectByExample(example);
        //删除系统与服务关联表
        systemServiceList.forEach(systemService -> systemServiceMapper.deleteByPrimaryKey(systemService.getSystemServiceId()));

        //删除此系统所绑定所有用户
        UserExample userExample = new UserExample();
        userExample.createCriteria().andSystemIdEqualTo(Integer.valueOf(deleteSystemPO.getSystemId()));
        List<User> userList = userMapper.selectByExample(userExample);

        userList.forEach(user -> userMapper.deleteByPrimaryKey(user.getUserId()));

        //删除此系统所绑定的规则
        RuleExample ruleExample = new RuleExample();
        ruleExample.createCriteria().andSystemIdEqualTo(Integer.valueOf(deleteSystemPO.getSystemId()));
        List<Rule> ruleList = ruleMapper.selectByExample(ruleExample);

        ruleList.forEach(rule -> ruleMapper.deleteByPrimaryKey(rule.getRuleId()));

        //删除系统
        systemMapper.deleteByPrimaryKey(Integer.valueOf(deleteSystemPO.getSystemId()));

        /*operation(deleteSystemPO, SystemDO.class, systemDO -> {

            //删除系统与服务关联表
            List<SystemServiceDO> systemServiceDOList = systemServiceDORepository.findBySystemId(systemDO.getSystemId()).orElse(Lists.newArrayList());
            systemServiceDOList.stream().filter(systemServiceDO -> systemServiceDO != null).forEach((systemServiceDO) -> {
                systemServiceDORepository.deleteById(systemServiceDO.getSystemServiceId());
            });
            //删除此系统所绑定所有用户
            List<UserDO> userDOList = userDORepository.findBySystemId(systemDO.getSystemId()).orElse(Lists.newArrayList());
            userDOList.stream().filter(userDO -> userDO != null).forEach((userDO) -> {
                userDORepository.deleteById(userDO.getUserId());
            });
            //删除此系统所绑定的规则
            List<RuleDO> ruleDOList = ruleDORepository.findBySystemId(systemDO.getSystemId()).orElse(Lists.newArrayList());
            ruleDOList.stream().filter(ruleDO -> ruleDO != null).forEach((ruleDO) -> {
                ruleDORepository.deleteById(ruleDO.getRuleId());
            });
            //删除系统
            systemDORepository.deleteById(systemDO.getSystemId());
        });*/
    }

    @Override
    public void deleteRule(DeleteRulePO deleteRulePO) {
        ValidatorUtil.validate(deleteRulePO);

        RuleRoleExample ruleRoleExample = new RuleRoleExample();
        ruleRoleExample.createCriteria().andRuleIdEqualTo(Integer.valueOf(deleteRulePO.getRuleId()));
        List<RuleRole> ruleRoleList = ruleRoleMapper.selectByExample(ruleRoleExample);

        ruleRoleList.forEach(ruleRole -> ruleRoleMapper.deleteByPrimaryKey(ruleRole.getRuleRoleId()));

        ruleMapper.deleteByPrimaryKey(Integer.valueOf(deleteRulePO.getRuleId()));

        /*operation(deleteRulePO, RuleDO.class, ruleDO -> {

            //删除规则与角色关联表
            List<RuleRoleDO> ruleRoleDOList = ruleRoleDORepository.findByRuleId(ruleDO.getRuleId()).orElse(Lists.newArrayList());
            ruleRoleDOList.stream().filter(ruleRoleDO -> ruleRoleDO != null).forEach((ruleRoleDO) -> {
                ruleRoleDORepository.deleteById(ruleRoleDO.getRuleRoleId());
            });
            //删除规则
            ruleDORepository.deleteById(ruleDO.getRuleId());
        });*/
    }


    private <T extends Object, DO extends SupperDO, E extends OperationFunction<DO>> void operation(T t, Class<DO> clazz, E e) {
        DO domain = BeanPlasticityUtill.copy(clazz, t);
        domain.timeSet();
        e.execute(domain);
    }

    public static void main(String[] args) {

        ArrayList<Integer> arrayList = new ArrayList<>();

        arrayList.forEach(integer -> java.lang.System.out.println(integer));
    }

}
